package com.huawei123.hasee.cniao5store.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.bean.AppBean;
import com.huawei123.hasee.cniao5store.bean.RecommendBean;
import com.zhxu.recyclerview.adapter.CommonAdapter;
import com.zhxu.recyclerview.adapter.MultiItemTypeAdapter;
import com.zhxu.recyclerview.base.ItemViewDelegate;
import com.zhxu.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * 项目名称：Cniao5Store
 * 类描述：推荐页的RecyclerView适配器
 * 创建人：luohao
 * 创建时间：2018/2/3 10:42
 * 修改人：luohao
 * 修改时间：2018/2/3 10:42
 * 修改备注：
 */

public class RecommendAdapter extends MultiItemTypeAdapter<RecommendBean.RecommendAppBean> {

    private Context context;
    private AppItemListener appItemListener;

    public void setAppItemListener(AppItemListener listener) {
        this.appItemListener = listener;
    }

    public RecommendAdapter(Context context, List<RecommendBean.RecommendAppBean> recommendAppBeanList) {
        super(context, recommendAppBeanList);
        this.context = context;
        addItemViewDelegate(new AppDelegate());
        addItemViewDelegate(new AdDetrgate());
    }

    /**
     * APP推荐 横向条目类型（普通横向列表）
     */
    class AppDelegate implements ItemViewDelegate<RecommendBean.RecommendAppBean> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_applist_horizontal;
        }

        @Override
        public boolean isForViewType(RecommendBean.RecommendAppBean item, int position) {
            return item.getType() == 0;
        }

        @Override
        public void convert(ViewHolder holder, final RecommendBean.RecommendAppBean recommendAppBean, int position) {
            holder.setText(R.id.mTV_item_title, recommendAppBean.getTitle());
            RecyclerView rv = holder.getView(R.id.mRV_applist_item);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(LinearLayout.HORIZONTAL);
            rv.setLayoutManager(linearLayoutManager);
            final AppItemAdapter appItemAdapter = new AppItemAdapter(context);
            appItemAdapter.addDataAll(recommendAppBean.getAppList());
            rv.setAdapter(appItemAdapter);
            //选择应用的点击事件
            appItemAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                    //recyclerview中的点击事件一般需要自定义在adapter中，
                    // 所以直接调用adapter中的setOnItemClickListener方法
                    //onItemClick方法参数中的position就是点击的位置，
                    // 同时可以在adapter中定义setSelectedPosition()方法设置点击的位置，
                    //然后在adapter中定义getSelectedPosition()获取被选中的位置
                    if (appItemListener!=null) {
                        appItemListener.getAppName(recommendAppBean.getAppList().get(position).getPackageName());
                        //1 、通过调用getSelectedPosition()方法获取上一次被选中的位置
                        //如果首次点击的话获取到的是-1
                        int selectedPosition = appItemAdapter.getSelectedPosition();
                        //如果是同一个位置就return
                        if (selectedPosition == position) {
                            return;
                        }
                        //如果不是同一个位置，再去做相关的业务逻辑，
                        // 最后调用setSelectedPosition方法将此次点击的位置设定为选中的位置
                        appItemAdapter.setSelectedPosition(position);
                    }
                }

                @Override
                public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                    return false;
                }
            });
        }
    }

    /**
     * ，双照片横向列表
     */
    class AdDetrgate implements ItemViewDelegate<RecommendBean.RecommendAppBean> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_ad;
        }

        @Override
        public boolean isForViewType(RecommendBean.RecommendAppBean item, int position) {
            return item.getType() == 1;
        }

        @Override
        public void convert(ViewHolder holder, RecommendBean.RecommendAppBean recommendAppBean, int position) {
            holder.setImageUrl(R.id.mIV_ad1, recommendAppBean.getIconList().get(0));
            holder.setImageUrl(R.id.mIV_ad2, recommendAppBean.getIconList().get(1));
        }
    }

    class AppItemAdapter extends CommonAdapter<AppBean> {

        public AppItemAdapter(Context context) {
            super(context, R.layout.item_app);
        }

        @Override
        protected void convert(ViewHolder holder, AppBean appBean, int position) {
            holder.setImageUrl(R.id.mIV_app_icon, appBean.getIcon());
            holder.setText(R.id.mTV_app_name, appBean.getName());
            holder.setText(R.id.mTV_app_size, appBean.getSizeDesc());
            holder.setTypeface(Typeface.DEFAULT_BOLD, R.id.mTV_app_name);//设置字体

        }

        //adapter中定义两个方法，用于设置选中的位置和获取选中的位置
        //定义变量用于存储被选中的位置，默认值-1
        private int selectedPosition = -1;

        public void setSelectedPosition(int position) {
            this.selectedPosition = position;
        }


        public int getSelectedPosition() {
            return selectedPosition;
        }
    }

    public interface AppItemListener {
        void getAppName(String name);
    }
}
